package com.shell.governor.audit.online.service.impl;

import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.audit.online.dao.PubOnlineDao;
import com.shell.governor.audit.online.dao.PubOnlineHistDao;
import com.shell.governor.audit.online.service.PubOnlineHistService;
import com.shell.governor.audit.po.PubOnline;
import com.shell.governor.audit.po.PubOnlineHist;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("pubOnlineHistServiceImpl")
public class PubOnlineHistServiceImpl extends PersistentServiceImpl<PubOnlineHist> implements PubOnlineHistService {
	@Autowired
	private PubOnlineHistDao onlineHistDao;
	@Autowired
	private PubOnlineDao onlineDao;

	public int saveOnlineHistRecord(PubOnline pubOnline) {
		List<PubOnline> onlines = new ArrayList<PubOnline>();
		onlines = this.onlineDao.select(pubOnline);
		int result = 0;
		List<String> deList;
		if ((onlines != null) && (onlines.size() > 0)) {
			deList = new ArrayList<String>();
			for (PubOnline online : onlines) {
				this.onlineDao.delete(online);
				if (!deList.contains(online.getOnlineId())) {
					deList.add(online.getOnlineId());
					PubOnlineHist hist = new PubOnlineHist();
					try {
						BeanUtils.copyProperties(hist, online);
					} catch (IllegalAccessException e) {
						e.printStackTrace();
					} catch (InvocationTargetException e) {
						e.printStackTrace();
					}
					hist.setLogoutTime(SysDateUtil.getSystemDateTime());
					List<PubOnlineHist> hists = new ArrayList<PubOnlineHist>();
					hists = this.onlineHistDao.select(hist);
					if ((hists == null) || (hists.size() == 0)) {
						result = this.onlineHistDao.insert(hist);
					}
				}
			}
		}
		return result;
	}
}
